package ru.tennisworld.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.tennisworld.user.dto.profile.UserProfileRequestDto;
import ru.tennisworld.user.dto.profile.UserProfileResponseDto;
import ru.tennisworld.user.dto.profile.UserProfileUpdateRatingRequestDto;
import ru.tennisworld.user.dto.profile.UserRatingReferenceDto;
import ru.tennisworld.user.entities.TennisUser;
import ru.tennisworld.user.entities.TennisUserProfile;
import ru.tennisworld.user.mappers.TennisUserProfileMapper;
import ru.tennisworld.user.service.TennisUserProfileControllerService;
import ru.tennisworld.user.service.users.TennisUserProfileService;
import ru.tennisworld.user.service.users.TennisUserService;

@Service
public class TennisUserProfileControllerServiceImpl implements TennisUserProfileControllerService {

    @Autowired
    private TennisUserService tennisUserService;

    @Autowired
    private TennisUserProfileService profileService;

    @Autowired
    private TennisUserProfileMapper profileMapper;

    private static final Logger LOG = LoggerFactory.getLogger(TennisUserProfileControllerServiceImpl.class);

    @Override
    public UserProfileResponseDto updateUserProfile(String userProfileId, UserProfileRequestDto request) {
        LOG.info("operation=TennisUserProfileControllerServiceImpl.updateUserProfile, action=start, request={}", request);
        TennisUserProfile srcProfile = profileMapper.mapToEntity(request);
        TennisUserProfile updProfile = profileService.getTennisUserProfile(Long.valueOf(userProfileId));
        srcProfile = profileService.updateTennisUserProfile(updProfile, srcProfile);
        UserProfileResponseDto result = profileMapper.mapToDto(srcProfile);
        LOG.info("operation=TennisUserProfileControllerServiceImpl.updateUserProfile, action=end, result={}", result);
        return result;
    }

    @Override
    public void deleteUserProfile(String userProfileId) {
        LOG.info("operation=TennisUserProfileControllerServiceImpl.deleteUserProfile, action=start, id={}", userProfileId);
        profileService.deleteTennisUserProfile(Long.valueOf(userProfileId));
        LOG.info("operation=TennisUserProfileControllerServiceImpl.deleteUserProfile, action=end");
    }

    @Override
    public void updateUserRating(UserProfileUpdateRatingRequestDto request) {
        LOG.info("operation=TennisUserProfileControllerServiceImpl.deleteUserProfile, action=start, request={}", request);
        for (int i = 0; i < request.getWinnerIds().size(); i++) {
            TennisUser winner = tennisUserService.getTennisUser(request.getWinnerIds().get(i));
            TennisUser loser = tennisUserService.getTennisUser(request.getLoserIds().get(i));
            tennisUserService.updateUserRating(winner, loser);
        }
        LOG.info("operation=TennisUserProfileControllerServiceImpl.deleteUserProfile, action=end");
    }

}
